/**
 * 
 */
package org.abrysov.android.lombarditens.activity;

import java.util.ArrayList;

import org.abrysov.android.lombarditems.MainActivity;
import org.abrysov.android.lombarditems.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author Alexander Brysov
 *
 */
public class StockList extends Activity {
	
	private static final String TAG = "_li_StockList.class";
	private ListView mStockList = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stocklist);
	
		mStockList = (ListView)findViewById(R.id.stocklist_lv);
		
		SQLiteDatabase db = openOrCreateDatabase(MainActivity.DATABASE_NAME, MODE_PRIVATE, null);
		
		final ArrayList<String> values = new ArrayList<String>();
		
		try {
			Cursor c = db.rawQuery("SELECT * FROM items", null);
			c.moveToFirst();
			//get 0 position
			values.add(c.getString(c.getColumnIndex("description")));
			
			// get 1 - END position
			while (c.moveToNext()) {
				values.add(c.getString(c.getColumnIndex("description")));
				Log.d(TAG, "list : " +c.getString(c.getColumnIndex("description"))+ "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		db.close();
		
		ArrayAdapter<String> aaStock = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);
	
		mStockList.setAdapter(aaStock);
		mStockList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
					
					Intent iItem = new Intent(getApplicationContext(), DescripteonItem.class);
					iItem.putExtra("nameItem", values.get(arg2));
					iItem.putExtra("positionItem", arg2);
					startActivity(iItem);
					
				
			}
		});
		
	}

	
	
}
